package edu.xlh.travel.service.impl;

import edu.xlh.travel.common.GalleryInRedisConstant;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.GallleryDao;
import edu.xlh.travel.dao.HotelDao;
import edu.xlh.travel.dao.ScenicDao;
import edu.xlh.travel.enumeration.SFTPOperateTypeEnum;
import edu.xlh.travel.enumeration.UploadFileServiceTypeEnum;
import edu.xlh.travel.pojo.Gallery;
import edu.xlh.travel.pojo.Hotel;
import edu.xlh.travel.pojo.Scenic;
import edu.xlh.travel.service.UploadFileService;
import edu.xlh.travel.util.FTPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UploadFileServiceImpl implements UploadFileService {

    @Value("http://${ftp-centos.host}")
    private String ftpCentosIp;
    @Autowired
    private GallleryDao gallleryDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HotelDao hotelDao;
    @Autowired
    private ScenicDao scenicDao;

    @Override
    public Result uploadFile(MultipartFile file , String uploadFileServiceType) throws Exception {
        try {
            //获取文件名
            String filename = file.getOriginalFilename();
            //log.info(filename);
            //起别名
            int index = filename.lastIndexOf(".");
            String suffix = filename.substring(index);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String dateNowStr = sdf.format(date);
            String prefix  = UUID.randomUUID().toString().replace("-", "");
            if (UploadFileServiceTypeEnum.galleryUpload.getCode().equals(uploadFileServiceType)){
                int nextImgNo = getNextImgNo(uploadFileServiceType);
                StringBuilder nextImgNoToStr = new StringBuilder(String.valueOf(nextImgNo));
                if (nextImgNo < 10) {
                    nextImgNoToStr.insert(0 , "0");
                }
                filename=dateNowStr+"_"+prefix+"_gallery"+nextImgNoToStr+suffix;
            } else if (UploadFileServiceTypeEnum.hotelPicUpload.getCode().equals(uploadFileServiceType)){
                int nextImgNo = getNextImgNo(uploadFileServiceType);
                StringBuilder nextImgNoToStr = new StringBuilder(String.valueOf(nextImgNo));
                if (nextImgNo < 10) {
                    nextImgNoToStr.insert(0 , "0");
                }
                filename=dateNowStr+"_"+prefix+"_hotel"+nextImgNoToStr+suffix;
            }  else if (UploadFileServiceTypeEnum.scenicPicUpload.getCode().equals(uploadFileServiceType)){
                int nextImgNo = getNextImgNo(uploadFileServiceType);
                StringBuilder nextImgNoToStr = new StringBuilder(String.valueOf(nextImgNo));
                if (nextImgNo < 10) {
                    nextImgNoToStr.insert(0 , "0");
                }
                filename=dateNowStr+"_"+prefix+"_scenic"+nextImgNoToStr+suffix;
            }
            //开始上传
            FTPUtil.sshSftp(file.getBytes(), filename , SFTPOperateTypeEnum.put.getCode());
             redisTemplate.boundSetOps(GalleryInRedisConstant.Gallery_RD_RESOURCES).add(filename);
            //log.info("最终文件的访问路径："+uploadVO.getFileName());
            return new Result(200 , "相册名返回成功",filename);
        } catch (IOException e) {
            //log.error("图片上传失败"+upload.getOriginalFilename());
            //throw new MyException(ResultCodeEnum.SERVICE_ERROR);
            System.out.println(e.getMessage());
            return new Result(202);
        }
    }

    public int getNextImgNo(String uploadFileServiceType) {
        if (UploadFileServiceTypeEnum.galleryUpload.getCode().equals(uploadFileServiceType)) {
            List<Gallery> galleryList;
            galleryList = gallleryDao.findAll( Sort.by(Sort.Direction.DESC, "id"));
            int to = galleryList.get(0).getImg().lastIndexOf(".");
            int from = galleryList.get(0).getImg().lastIndexOf("gallery")+7;
            return Integer.parseInt(galleryList.get(0).getImg().substring(from, to))+1;
        } else  if(UploadFileServiceTypeEnum.hotelPicUpload.getCode().equals(uploadFileServiceType)) {
            List<Hotel> hotelList;
            hotelList = hotelDao.findAll( Sort.by(Sort.Direction.DESC, "id"));
            if (hotelList == null || hotelList.size() == 0) {
                return 0;
            }
            int to = hotelList.get(0).getImg().lastIndexOf(".");
            int from = hotelList.get(0).getImg().lastIndexOf("hotel")+5;
            return Integer.parseInt(hotelList.get(0).getImg().substring(from, to))+1;
        } else if (UploadFileServiceTypeEnum.scenicPicUpload.getCode().equals(uploadFileServiceType)) {
            List<Scenic> scenicList;
            scenicList = scenicDao.findAll( Sort.by(Sort.Direction.DESC, "id"));
            if (scenicList == null || scenicList.size() == 0) {
                return 0;
            }
            int to = scenicList.get(0).getImg().lastIndexOf(".");
            int from = scenicList.get(0).getImg().lastIndexOf("scenic")+6;
            return Integer.parseInt(scenicList.get(0).getImg().substring(from, to))+1;
        } else {
            return -1;
        }
    }
}
